package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.album.vo.CategoryVo;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	BaseAttributeMapper attributeMapper;

//	@Override
//	public List<CategoryVo> getBaseCategoryList() {
//		List<CategoryVo> list = new ArrayList<>();
//		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
//		Map<Long, List<BaseCategoryView>> baseCate1 = baseCategoryViews
//				.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
//
//		for (Map.Entry<Long, List<BaseCategoryView>> listEntrybase1 : baseCate1.entrySet()) {
//			CategoryVo categoryVo = new CategoryVo();
//			categoryVo.setCategoryId(listEntrybase1.getKey());
//			String categoryName1 = listEntrybase1.getValue().get(0).getCategory1Name();
//			categoryVo.setCategoryName(categoryName1);
//			//二级分类的集合分组
//			Map<Long, List<BaseCategoryView>> listEntrybase2 = listEntrybase1.getValue()
//					.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
//			List<CategoryVo>list2 = new ArrayList<>();
//			for (Map.Entry<Long, List<BaseCategoryView>> listEntrybaseMap2 : listEntrybase2.entrySet()) {
//				CategoryVo categoryVo2 = new CategoryVo();
//				categoryVo2.setCategoryId(listEntrybaseMap2.getKey());
//				categoryVo2.setCategoryName(listEntrybaseMap2.getValue().get(0).getCategory2Name());
//				//吧三集放到二级中
//				ArrayList<CategoryVo> list3 = new ArrayList<>();
//				for (BaseCategoryView baseCategoryView3 : listEntrybaseMap2.getValue()) {
//					CategoryVo categoryVo3 = new CategoryVo();
//					categoryVo3.setCategoryId(baseCategoryView3.getCategory3Id());
//					categoryVo3.setCategoryName(baseCategoryView3.getCategory3Name());
//					categoryVo3.setCategoryChild(null);
//
//					list3.add(categoryVo3);
//
//				}
//				categoryVo2.setCategoryChild(list3);
//
//				list2.add(categoryVo2);
//			}
//			//1的孩子
//			categoryVo.setCategoryChild(list2);
//			//1的所有集合
//			list.add(categoryVo);
//
//		}
//
//		return list;
//	}

	@Override
	public List<CategoryVo> getBaseCategoryList() {
	 List<CategoryVo>categoryList = baseCategoryViewMapper.getBaseCategoryList();
	 if (CollectionUtils.isEmpty(categoryList)){
		 return new ArrayList<>();
	 }
		return categoryList;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long c1Id) {
		return attributeMapper.findAttribute(c1Id);
	}


	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long c1Id) {
//		LambdaQueryWrapper<BaseCategory2>wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(BaseCategory2::getCategory1Id,c1Id);
//		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(wrapper);

//		if (!CollectionUtils.isEmpty(baseCategory2s)){
//			List<Long> baseId2List = baseCategory2s.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());
//			LambdaQueryWrapper<BaseCategory3>wrappers = new LambdaQueryWrapper<>();
//			wrappers.eq(BaseCategory3::getCategory2Id,baseId2List);
//			wrappers.eq(BaseCategory3::getIsTop,1);
//			wrappers.last("limit 7");
//			List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrappers);
//			return baseCategory3List;
//		}
//		return null;
		// 根据一级分类  找到这个一级分类下的三级分类集合

		// 1.根据一级分类id找到二级分类
		LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper();
		wrapper.eq(BaseCategory2::getCategory1Id, c1Id);
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(wrapper);

		if (!CollectionUtils.isEmpty(baseCategory2s)) {
			// 获取二级分类的id
			List<Long> c2Ids = baseCategory2s.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());
			LambdaQueryWrapper<BaseCategory3> wrapper1 = new LambdaQueryWrapper<>();

			wrapper1.in(BaseCategory3::getCategory2Id, c2Ids);
			wrapper1.eq(BaseCategory3::getIsTop, 1);
			wrapper1.last("limit 7");
			List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(wrapper1);
			return baseCategory3s;
		}


		return null;
	}

	@Override
	public BaseCategoryView getCategoryById123rpc(Long id) {
	 return baseCategoryViewMapper.getCategoryById123rpc(id);
	}
}
